<?php

class FeedbackController extends Zend_Controller_Action {

	public function indexAction() {
		$form = new Default_Form_Feedback();
		$form->setAction($this->view->url(array(),'default_feedback_index'));
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			$values = $form->getValues();
			
			$feedback = new Default_Model_Feedback();
			$feedback->email = $values['email'];
			$feedback->message = $values['message'];
			$feedback->created_at = time();
			$feedback->is_read = false;
			$feedback->username = $values['username'];
			$feedback->save();
			
			$settings = Doctrine_Core::getTable('Default_Model_Settings')->find(1);
			if($settings->send_email_feedback == true) {
				$mail = new Zend_Mail('KOI8-R');
				$mail->setBodyText(htmlspecialchars(iconv('utf-8', 'KOI8-R', $values ['message'])));
				$mail->setSubject('=?KOI8-R?B?'.base64_encode(iconv('utf-8', 'KOI8-R', substr($values['message'], 0, 120) . '...')).'?=');
				
				$emails = explode(",", $settings->email_list );
				foreach($emails as $key=>$item) {
					if(trim($item) != '') {
						$mail->addTo(trim($item));	
					}		
				}
				$mail->send();
			}
			
			$this->_helper->getHelper('FlashMessenger')
					->addMessage(_('Message has sent successfully!'));
			return $this->_redirect($this->view->url(array(), 'default_feedback_index'));
		}
		
		$this->view->form = $form;
	}
	
	
	public function listAction() {
		$listing = Doctrine_Query::create()
					->from('Default_Model_Feedback')
					->orderBy('is_read ASC')
					->addOrderBy('created_at DESC')
					->execute();
		$this->view->listing = $listing;
	}
	
	public function deleteAction() {
		$id = $this->_request->getParam('id', 0);
		if(!$id) {
			$this->_helper->getHelper('FlashMessenger')
					->addMessage($this->view->translate('Message not found!'));
			return $this->_redirect($this->view->url(array(), 'default_feedback_list'));
		}
		
		$message = Doctrine_Core::getTable('Default_Model_Feedback')->find($id);
		$message->delete();
		
		$this->_helper->getHelper('FlashMessenger')
					->addMessage($this->view->translate('Message was delete successfully!'));
		return $this->_redirect($this->view->url(array(), 'default_feedback_list'));
	}
	
	public function showAction() {
		$id = $this->_request->getParam('id', 0);
		if(!$id) {
			$this->_helper->getHelper('FlashMessenger')
					->addMessage($this->view->translate('Message not found!'));
			return $this->_redirect($this->view->url(array(), 'default_feedback_list'));
		}
		$message = Doctrine_Core::getTable('Default_Model_Feedback')->find($id);
		$message->is_read = true;
		$message->save();
		
		$this->view->message = $message;
	}
	
	public function settingAction() {
		$form = new Default_Form_Feedbackset();
		$form->setAction($this->view->url(array(), 'default_feedback_setting'));
		
		$settings = Doctrine_Core::getTable('Default_Model_Settings')->find(1);
		if($settings) {
			$populate = array(
				'emails' => $settings->email_list,
				'redirect' => $settings->send_email_feedback
			);
		} else {
			$populate = array(
				'emails' => '',
				'redirect' => 'yes'
			);
		}
				
		$this->view->form = $form;
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			$values = $form->getValues();
			
			if(!$settings) {
				$settings = new Default_Model_Settings();
			}
			
			$settings->email_list = $values['emails'];
			$settings->send_email_feedback = $values['redirect'];
			$settings->save();
			$this->_helper->getHelper('FlashMessenger')
					->addMessage($this->view->translate('Settings feedback save successfully!'));
			return $this->_redirect($this->view->url(array(), 'default_feedback_setting'));
		} else {
			$form->populate($populate);
		}

	}
	
}